package org.biubiu0002.baiblog.im.handler;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @Author: biubiu0002
 * @Date: 2022/10/24 2:04
 **/
@Slf4j
@Component
@ChannelHandler.Sharable
public class HeartbeatHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
            if (idleStateEvent.state() == IdleState.READER_IDLE) {
                log.info("进入读空闲...");
                Channel channel = ctx.channel();
                //关闭无用channel，避免浪费资源
                channel.close();
            }
//            else if (idleStateEvent.state() == IdleState.WRITER_IDLE) {
//                log.info("进入写空闲...");
//            } else if (idleStateEvent.state() == IdleState.ALL_IDLE) {
//                log.info("进入读写空闲...");
//
//            }
        }
        super.userEventTriggered(ctx, evt);
    }
}
